using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TVDesigner.Generator;

namespace TVDesigner.Gui
{
    public partial class FMain : Form
    {
        private ParseElements _ParseElements;

        public ParseElements ParseElements
        {
            get { return _ParseElements; }
            set { _ParseElements = value; }
        }


        public FMain()
        {
            InitializeComponent();
            this.chkIsSystemvariables.Checked = true;
            this.rdoSepareteFolders.Checked = true;
        }

        private void btnPath_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == folderBrowserDialog.ShowDialog())
            {
                this.txtLocation.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            if (ValidateFields())
            {
                defaultStatusBar1.StartBusy("Creating project...");
                LockControls();

                GenerateInstructions instructions = new GenerateInstructions();
                instructions.ProjectName = txtProjectName.Text;
                instructions.Location = txtLocation.Text;
                instructions.SameFolder = rdoSameFolder.Checked;
                instructions.ParseElements = _ParseElements;

                instructions.HasSystemVariables = this.chkIsSystemvariables.Checked;
                instructions.JavacLocation = txtJavaLocation.Text;

                bwGenerate.RunWorkerAsync(instructions);
            }
            else
            {
                MessageBox.Show("Falta validar campos.");
            }
        }


        private void bwGenerate_DoWork(object sender, DoWorkEventArgs e)
        {
            GenerateInstructions instructions = (GenerateInstructions)e.Argument;
            
            Generator.Generator generator = Generator.Generator.Instance;
            generator.CreateProject(instructions);
        }

        private void bwGenerate_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            defaultStatusBar1.EndBusy();
            UnLockControls();
        }


        private bool ValidateFields()
        {
            bool result = true;

            if (!((txtLocation.Text.Length > 0) && (txtProjectName.Text.Length > 0)))
                result = false;

            if (txtJavaLocation.Text.Length <= 0 && !chkIsSystemvariables.Checked)
                result = false;

            return result;
        }

        private void UnLockControls()
        {
            this.SetControlsEnabled(true);
        }
        private void LockControls()
        {
            this.SetControlsEnabled(false);
        }
        private void SetControlsEnabled(bool enabled)
        {
            foreach (Control control in this.Controls)
            {
                control.Enabled = enabled;
            }

            defaultStatusBar1.Enabled = true;
        }

        private void btnJavaLocation_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == openFileDialogJava.ShowDialog())
            {
                this.txtJavaLocation.Text = openFileDialogJava.FileName;
            }
        }

        private void chkIsSystemvariables_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkIsSystemvariables.Checked)
            {
                this.lblJavaLocation.Enabled = false;
                this.txtJavaLocation.Enabled = false;
                this.btnJavaLocation.Enabled = false;
            }
            else
            {
                this.lblJavaLocation.Enabled = true;
                this.txtJavaLocation.Enabled = true;
                this.btnJavaLocation.Enabled = true;
            }
        }


    }
}